﻿


function openModal(sURL, sWidth, sHeight) {
    var sParameters;
    sParameters = "dialogHeight: " + sHeight + "px; dialogWidth: " + sWidth + "px; edge: Raised; center: Yes; help: No; resizable: No; status: No; scroll: No;";
    var windowReturnValue = window.showModalDialog(sURL, window, sParameters);
    return windowReturnValue;
}
function openModalWithArgs(sURL, arg, sWidth, sHeight) {
    var sParameters;
    sParameters = "dialogHeight: " + sHeight + "px; dialogWidth: " + sWidth + "px; edge: Raised; center: Yes; help: No; resizable: No; status: No; scroll: No;";
    var windowReturnValue = window.showModalDialog(sURL, arg, sParameters);
    return windowReturnValue;
}
/*
*   popover modal dialog management (very like openModal except it pops Over this window and not Up from it)
*/
// simple request to pop a Modal Dialog over the current web form
function openModalPopover(sURL, sWidth, sHeight, sTitle) {
    openModalPopoverWithResponse(sURL, sWidth, sHeight, sTitle, null);
}
// pop over modal request, possibly expecting asynchronous response at some future time
function openModalPopoverWithResponse(sURL, sWidth, sHeight, sTitle, responder) {
    var myDoc = top.document;
    top.popOverResponseHandler = responder;
    top.modalReturnValue = "";
    sWidth = parseInt(sWidth);
    sHeight = parseInt(sHeight);
    var divTitleHeight = 40;    // the div element needs vertical space for an injected top bar
    var iframeFudge = 6;        // the iframe also needs to stand higher than caller's target page size. (why? borders?)
    var iframeWidthIncrem = 6;  // make iframe wider as well, avoiding scroll bars
    var divWidthIncrem = 0;
    var frmHeight = sHeight + iframeFudge;
    var frmWidth = sWidth + iframeWidthIncrem;
    var divHeight = frmHeight + divTitleHeight;
    var divWidth = frmWidth + divWidthIncrem;
    var popDiv = myDoc.createElement("div");
    popDiv.setAttribute("id", "popOver");
    popDiv.setAttribute("title", sTitle);
    popDiv.style.setAttribute("margin", "0px");
    popDiv.style.setAttribute("padding", "0px");

    myDoc.body.appendChild(popDiv);
    var ifrm = myDoc.createElement("iframe");
    ifrm.setAttribute("id", "modalFrame");
    ifrm.setAttribute("FRAMEBORDER", "0");
    ifrm.setAttribute("SCROLLING", "No");
    ifrm.setAttribute("height", frmHeight);
    ifrm.setAttribute("width", frmWidth - 9);

    ifrm.style.setAttribute("margin", "0px");
    ifrm.style.setAttribute("padding", "0px");
    ifrm.setAttribute("src", sURL);
    popDiv.appendChild(ifrm);
    top.openGlobalDialog(sURL, divWidth, divHeight, sTitle);
    // after jQuery 1.7 finishes doing the best it can, we need to adjust vertical size/position
    popDiv.style.setAttribute("height", frmHeight);
    var popTop = 8;
    var docDepth = getDocHeight();
    if (docDepth > divHeight) {
        popTop = (docDepth - divHeight) / 2;
    }
    popDiv.parentElement.style.setAttribute("top", popTop);
}
// acquire overhall height of browser window (not so easy as you imagine, and very browser sensitive)
function getDocHeight() {
    var db = top.document.body;
    var dde = top.document.documentElement;
    return Math.max(db.offsetHeight, dde.offsetHeight
            , db.clientHeight, dde.clientHeight);
};

// get the response from some web page, where input is all in Url query string
function queryWebResource(sURL) {
    return requestWebResource("GET", "", sURL);
}
// Post my xml request to some web page and acquire its response
function postWebResource(requestXml, sURL) {
    return requestWebResource("POST", requestXml, sURL);
}
function requestWebResource(verb, requestXml, sURL) {
    var result = "";
    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
    xmlhttp.Open(verb, sURL, false);
    xmlhttp.Send(requestXml);
    result = xmlhttp.responseText;
    return result;
}

function openGlobalDialog(sURL, sWidth, sHeight, sTitle) {
    $("#popOver").dialog({
        autoOpen: false
        , modal: true
        , height: sHeight, width: sWidth
        , overlay: {
            opacity: 0.5,
            background: "black"
        }
    });
    $("#popOver").bind("dialogclose", ClosePopOver);
    $("#popOver").dialog("open");
}
// this global public function is exposed for modal popover dialog to call, when it is finished
function popOverWindowClose() {
    $("#popOver").dialog("close");
    // which (btw) in turn drives: ClosePopOver();
}
// finally get rid of the popover dialog, no matter how it was terminated
function ClosePopOver(event, ui) {
    $("#popOver").dialog("destroy");
    $("#popOver").remove();
    if (null != top.popOverResponseHandler) {
        (top.popOverResponseHandler)(top.modalReturnValue);
        top.popOverResponseHandler = null;
    }
}